package perf;

import static editor.Configuration.BG_COLOUR;
import static editor.Configuration.TEXT_COLOUR;
import editor.Images;
import editor.code.LineField;
import editor.code.NumberField;
import editor.headers.Header;
import static editor.headers.Header.*;
import editor.headers.HeaderComponent;
import editor.widgets.ObservedStringComboBox;
import model.Project;
import model.caret.CaretRef;
import model.code.CodeFileChunk;
import model.code.Function;
import widgets.ImageButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Neil Dickson
 */
public class PerformanceTestOptions extends HeaderComponent {
	private final PerformanceTestInfo 		testInfo;
	private final ImageButton				runButton;
	private final ObservedStringComboBox	scaling;
	private final NumberField				maxNumResults;
	private final LineField					groupName;

	public PerformanceTestOptions(Header header,Object testInfo) {
		super(header);
		final Function function = (Function)header.getCode();
		final Project project = function.getProject();
		this.testInfo = (PerformanceTestInfo)testInfo;
		runButton = new ImageButton(Images.PERFORMANCE_RUN);
		runButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PerformanceTestEncoder.instance.runPerformanceTest(project,function);
			}
		});
		// NOTE: The combo boxes don't render properly with transparency
		scaling = new ObservedStringComboBox(this.testInfo.scaling,PerformanceTestInfo.SCALINGS);
		scaling.setBackground(BG_COLOUR.getColour());//TRANSPARENT_COLOUR);
		scaling.setForeground(TEXT_COLOUR.getColour());
		scaling.setFont(OTHER_FONT);
		CaretRef ref = header.getCode().getFile().getCaretRef();
		maxNumResults = new NumberField(this.testInfo.maxNumTestResults,10,100000,ref);
		maxNumResults.setBorder(TEXT_FIELD_BORDER);
		maxNumResults.setBackground(TRANSPARENT_COLOUR);
		maxNumResults.setForeground(TEXT_COLOUR.getColour());
		maxNumResults.setFont(OTHER_FONT);
		groupName = new LineField(this.testInfo.testGroupName,ref,true);
		groupName.setBorder(TEXT_FIELD_BORDER);
		groupName.setBackground(TRANSPARENT_COLOUR);
		groupName.setForeground(TEXT_COLOUR.getColour());
		groupName.setFont(OTHER_FONT);
		add(runButton);
		add(scaling);
		add(maxNumResults);
		add(groupName);
		setBackground(TRANSPARENT_COLOUR);
		setOpaque(false);
	}
	public int getType() {
		return CodeFileChunk.LOCATION_PARAMETERS;
	}
	public void doLayout(int width) {
		int x = 0;
		int height = 0;
		if (runButton.isVisible()) {
			runButton.setLocation(x,0);
			x += runButton.getWidth();
			height = Math.max(height,runButton.getHeight());
		}
		if (scaling.isVisible()) {
			if (scaling.getWidth()==0 || scaling.getHeight()==0) {
				scaling.setSize(scaling.getPreferredSize());
			}
			scaling.setLocation(x,0);
			x += scaling.getWidth();
			height = Math.max(height,scaling.getHeight());
		}
		if (maxNumResults.isVisible()) {
			if (maxNumResults.getWidth()==0 || maxNumResults.getHeight()==0) {
				maxNumResults.setSize(maxNumResults.getPreferredSize());
			}
			maxNumResults.setLocation(x,0);
			x += maxNumResults.getWidth();
			height = Math.max(height,maxNumResults.getHeight());
		}
		if (groupName.isVisible()) {
			if (groupName.getWidth()==0 || groupName.getHeight()==0) {
				groupName.setSize(groupName.getPreferredSize());
			}
			groupName.setLocation(x,0);
			x += groupName.getWidth();
			height = Math.max(height,groupName.getHeight());
		}
		setSize(x,height);
	}
	public int getMinWidth() {
		int width = 0;
		if (runButton.isVisible()) {
			width += runButton.getWidth();
		}
		if (scaling.isVisible()) {
			if (scaling.getWidth()==0 || scaling.getHeight()==0) {
				scaling.setSize(scaling.getPreferredSize());
			}
			width += scaling.getWidth();
		}
		if (maxNumResults.isVisible()) {
			if (maxNumResults.getWidth()==0 || maxNumResults.getHeight()==0) {
				maxNumResults.setSize(maxNumResults.getPreferredSize());
			}
			width += maxNumResults.getWidth();
		}
		if (groupName.isVisible()) {
			if (groupName.getWidth()==0 || groupName.getHeight()==0) {
				groupName.setSize(groupName.getPreferredSize());
			}
			width += groupName.getWidth();
		}
		return width;
	}
}
